package com.jyh.util;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ThreadUtils {
	private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>() {
		protected Map<String, Object> initialValue() {
			return new HashMap<>();
		};
	};

	public static void createLogId() {
		saveVal("logId", UUID.randomUUID().toString().replace("-", ""));
	}

	/**
	 * 获取LogId
	 * 
	 * @return
	 */
	public static String logId() {
		return getVal("logId") + "";
	}

	public static void remove() {
		threadLocal.remove();
	}

	/**
	 * 保存一个对象到线程变量中
	 */
	public static void saveVal(String key, Object val) {
		threadLocal.get().put(key, val);
	}

	/**
	 * 获取一个变量
	 * 
	 * @param key
	 * @return
	 */
	public static Object getVal(String key) {
		return threadLocal.get().get(key);
	}
}
